package com.FTW.SAA.controller;

import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions;
import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/embed")
public class Embed2VectorController {

    @Resource
    private EmbeddingModel embeddingModel;

    @Resource
    private VectorStore vectorStore;

    @GetMapping("/text2Embed")
    public EmbeddingResponse Text2Embed(@RequestParam(value = "msg") String message) {

        EmbeddingResponse embeddingResponse = embeddingModel.call(
                new EmbeddingRequest(List.of(message),
                DashScopeEmbeddingOptions.builder().withModel("text-embedding-v4").build()));

        System.out.println(Arrays.toString(embeddingResponse.getResult().getOutput()));

        return embeddingResponse;
    }

    @GetMapping("/add2VectorStore")
    public void Add2VectorStore() {

        List<Document> documentList = List.of(
                new Document("Learn LLM To Become A Better Guy"),
                new Document("Though I Know It is Hard To Get")
        );

        vectorStore.add(documentList);
    }

    @GetMapping("/getValueFromVectorStore")
    public String GetValueFromVectorStore() {

        SearchRequest searchRequest = SearchRequest.builder()
                .query("Learn LLM To Become A Better Guy")
                .topK(2)
                .build();

        List<Document> results =
                this.vectorStore.similaritySearch(searchRequest);

        return results.getFirst().getText();
    }
}
